<?php
/**
 * 1、采集铃声列表 current
 * 2、采集铃声内容
 */
include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Http.class.php');
include_once('../lib/function.php');

$redis = new Redis();  
$ret = $redis->connect("localhost","6379");  //php客户端设置的ip及端口
if(!$ret) die('redis connction faild');
$redis->auth(REDIS_PASS);
$redis->select(1);

$GLOBALS['config']['master']['db_database'] = 'mh_lnpan';
$GLOBALS['config']['slave']['db_database']  = 'mh_lnpan';

//$model = new Model('http://www.lnpan.com/index.php?m=api');
$model = new Db;
$Http  = new Http;

$page = 1;
$start_id = 0;

while (true) {
    $stop = $redis->get('LNPAN_QUEUE_STOP');
    if($stop) {
        sleep(3);
        die('stop');
    }

    $rs_link = $model->find("ring_news_link", "id>$start_id and state=1", "id asc");
    $ringUrl = $rs_link['link'];
    if(empty($ringUrl)) {
        $redis->lPush('LNPAN_FETCH_RINGLIST', serialize(array('time'=>date('Y-m-d H:i:s'), 'end', 'start_id'=>$start_id)));

        echo $start_id;
        sleep(600);
        die(' url null, the end.');
    }

    if($page > 1) {
        $ringUrl = str_replace('1.htm', $page.'.htm', $ringUrl);
        $ringUrl = str_replace('page=1', 'page='.$page, $ringUrl);
    }
    $parse = parse_url($ringUrl);
    $info  = array();
    $info['time'] = date('Y-m-d H:i:s');

    echo "$ringUrl \n";
    $responses = $Http->curl_http( array($ringUrl) );
    if(empty($responses[$ringUrl]['error']) && $responses[$ringUrl]['info']['url'] == $ringUrl) {
        $file = $responses[$ringUrl]['results'];
        $file = trim($file, '),callback(');
        $file = json_decode($file, true);

        if(empty($rs_link['type_name'])) {
            preg_match('/<strong>(.+?)<\/strong>/i', $file, $match);
            $info['name'] = trim($match[1]);
        } else {
            $info['name'] = trim($rs_link['type_name']);
        }

        foreach ($file['result'] as $_item) {
            $url = 'http://'.$parse['host'].'/lingsheng/'.$_item['shorturl'].'.htm';
            echo "$url \n";

            $name = $_item['topic'];
            $tmp = array(
                'name' => $name,
                'type_name' => $info['name'],
                'url' => $url,
                'format_name' => '',
                'tags' => '',
                'singers' => '',
                'down_url' => '',
                'down_info' => '',
            );
            $old = $model->find("ring_news_copy", "url='$url'");
            if(empty($old)) {
                $info['new_count']++;
                $news_id = $model->add("ring_news_copy", $tmp);
                if(!$news_id) {
                    $news_id = $model->error();
                } else {
                    $news_id = $model->insert_id();
                }
                $info[] = 'new:'.$news_id.':'.$rs_link['id'];
            } else {
                $info[] = 'old:'.$old['id'].':'.$rs_link['id'];
            }
        }

        //前3页没有新纪录跳过
        if($page >= 2 && $info['new_count'] == 0 || $page > 10) {
            $info['next_start_id'] = $start_id = $rs_link['id'];
            $info['next_page'] = $page = 1;
        } else {
            $info['next_start_id'] = $start_id;
            $page++;
            $info['next_page'] = $page;
        }

    } else {
        $info['error'] = $responses[$ringUrl]['error'].$responses[$ringUrl]['info']['url'];
        $info['next_start_id'] = $start_id = $rs_link['id'];
        $page = 1;
    }

    $redis->lPush('LNPAN_FETCH_RINGLIST', serialize($info));

    print_r($info);
    //sleep(20);
}
